/*
 * @author: wizardc
 */

using Dou.ScreenController;
using UnityEditor;
using UnityEngine;

namespace DouEditor.ScreenController
{
    [CustomEditor(typeof(RectTransformAdapter))]
    public class RectTransformAdapterEditor : Editor
    {
        private RectTransformAdapter _target;
        private RectTransform _rectTransform;

        private void OnEnable()
        {
            _target = (RectTransformAdapter) target;
            _rectTransform = _target.GetComponent<RectTransform>();

            InitAdapterData();
            TryRecordAdapterData();
        }

        private void InitAdapterData()
        {
            const int fineTuningMax = (int) UIFineTuningMode.Max;
            if (_target.rectTransformDataList.Count == 0)
            {
                for (int i = 0; i < (int) UIScreenMode.Max; i++)
                {
                    var data = new RectTransformData();
                    for (int j = 0; j < fineTuningMax; j++)
                    {
                        data.fineTuningList.Add(new RectTransformFineTuningData());
                    }
                    _target.rectTransformDataList.Add(data);
                }
            }
            if (_target.rectTransformDataList[0].fineTuningList.Count != fineTuningMax)
            {
                for (int i = 0; i < (int) UIScreenMode.Max; i++)
                {
                    var list = _target.rectTransformDataList[i].fineTuningList;
                    if (list.Count > fineTuningMax)
                    {
                        list.RemoveRange(0, fineTuningMax);
                    }
                    else
                    {
                        for (int j = list.Count; j < fineTuningMax; j++)
                        {
                            list.Add(new RectTransformFineTuningData());
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 如果当前横竖屏下没有记录数据，则以当前的数据作为基础数据进行记录
        /// </summary>
        private void TryRecordAdapterData()
        {
            var uiScreenMode = (int) ScreenControllerMgr.GetScreenMode();
            var data = _target.rectTransformDataList[uiScreenMode];
            if (!data.isInit)
            {
                data.isInit = true;
                RectTransformAdapterUtil.RecodeFormRectTransform(data, _rectTransform);
            }
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            TryRecordAdapterData();
            
            var uiScreenMode = (int) ScreenControllerMgr.GetScreenMode();
            var uiFineTuningMode = ScreenControllerMgr.GetFineTuningMode();
            var data = _target.rectTransformDataList[uiScreenMode];
            if (uiFineTuningMode == UIFineTuningMode.None)
            {
                RectTransformAdapterUtil.RecodeFormRectTransform(data, _rectTransform);
            }
            else
            {
                RectTransformAdapterUtil.RecodeFormRectTransform(data, _rectTransform, (int) uiFineTuningMode);
            }
            serializedObject.ApplyModifiedProperties();
        }
    }
}
